﻿Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class Control_Menu_LeftCategory
    Inherits System.Web.UI.UserControl
    '  Public NodeExpanded As New ArrayList
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            FillParentMenu()
        End If
    End Sub
    'Load du lieu cho menu cha
    Protected Sub FillParentMenu()
        Using data As New VSEntities
            Dim query = From q In data.Categories Where q.ParentId = 0 Order By q.Sort Ascending Select q
            Dim dtList = Query.ToList()
            trvMenu.Nodes.Clear()
            For i As Integer = 0 To dtList.Count - 1
                Dim tnParent As New TreeNode()
                tnParent.Text = dtList(i).Title
                tnParent.Value = dtList(i).CatId.ToString()
                'If Check_Selected(dtList(i).CatId) = False Then
                '    tnParent.SelectAction = TreeNodeSelectAction.None
                'End If
                trvMenu.Nodes.Add(tnParent)
                FillChild(tnParent, Int32.Parse(tnParent.Value))
            Next
        End Using
    End Sub
    'Load du lieu cho menu con
    Protected Sub FillChild(ByVal Parent As TreeNode, ByVal Value As Integer)
        Using data As New VSEntities
            Dim query = From q In data.Categories Where q.ParentId = Value Order By q.Sort Ascending Select q
            Dim dtList = query.ToList()
            Parent.ChildNodes.Clear()
            For i As Integer = 0 To dtList.Count - 1
                Dim child As New TreeNode()
                child.Text = dtList(i).Title
                child.Value = dtList(i).CatId.ToString()
                'If Check_Selected(dtList(i).CatId) = False Then
                '    child.SelectAction = TreeNodeSelectAction.None
                'End If
                Parent.ChildNodes.Add(child)

                Dim NodeExpanded As New ArrayList
                If Not Session("ExpandedNode") Is Nothing Then
                    NodeExpanded = Session("ExpandedNode")
                    'check neu MenuId in session
                    If NodeExpanded.Contains(Parent.Value) Then
                        Parent.Expand()
                    End If
                End If

                FillChild(child, Int32.Parse(child.Value))
            Next
        End Using

    End Sub

    Protected Sub trvMenu_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles trvMenu.SelectedNodeChanged
        If Check_Selected(trvMenu.SelectedValue) Then
            Response.Redirect("../../Page/Product/List.aspx?CateId=" & trvMenu.SelectedValue)
        Else
            Response.Redirect("../../Page/Product/List.aspx")
        End If
    End Sub

    Protected Sub trvMenu_TreeNodeCollapsed(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles trvMenu.TreeNodeCollapsed
        Dim NodeExpanded As New ArrayList
        If Not Session("ExpandedNode") Is Nothing Then
            NodeExpanded = Session("ExpandedNode")
            NodeExpanded.Remove(e.Node.Value)
            Session("ExpandedNode") = NodeExpanded
        End If

    End Sub

    Protected Sub trvMenu_TreeNodeExpanded(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles trvMenu.TreeNodeExpanded

        Dim NodeExpanded As New ArrayList

        If Session("ExpandedNode") Is Nothing Then
            NodeExpanded.Add(e.Node.Value)
            Session("ExpandedNode") = NodeExpanded
        Else
            NodeExpanded = Session("ExpandedNode")
            'neu node chua co' thi add vao ExpandedNode
            If NodeExpanded.Contains(e.Node.Value) = False Then
                NodeExpanded.Add(e.Node.Value)
            End If
            Session("ExpandedNode") = NodeExpanded
        End If

    End Sub

End Class
